using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace BattleCards
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        

        //mouse
        MouseState mouse; 
        Texture2D cursor;
        Vector2 posicao;        

        //botoes
        Texture2D  botao_jogar, botao_instrucoes, botao_voltar, botao_voltar_click,  botao_jogar_click, botao_instrucoes_click;

        enum Botoes { NORMAL, CLICK, }

        Botoes botao_atual;

        //telas
        enum Telas { INTRO, INSTRUCAO, JOGO, };

        Telas tela_atual;

        Texture2D telaIntroducao, telaInstrucao, telaJogo; 

        //jogo
        Jogo jogo;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            graphics.PreferredBackBufferHeight = 600;
            graphics.PreferredBackBufferWidth = 800;
            IsMouseVisible = false;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            
            //mouse
            posicao = new Vector2(graphics.PreferredBackBufferWidth / 2, graphics.PreferredBackBufferHeight / 2);
            
            //tela
            tela_atual = Telas.INTRO;

            //botoes
            botao_atual = Botoes.NORMAL;

            //jogo
            jogo = new Jogo();
          

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            telaIntroducao = Content.Load<Texture2D>("tela_fundo");
            telaInstrucao = Content.Load<Texture2D>("img_instrucao");
            cursor = Content.Load<Texture2D>("icon_sword");
            botao_jogar = Content.Load<Texture2D>("bot_iniciar");
            botao_instrucoes = Content.Load<Texture2D>("bot_instrucao");
            botao_jogar_click = Content.Load<Texture2D>("bot_iniciar_click");
            botao_instrucoes_click = Content.Load<Texture2D>("bot_instrucao_click");
            botao_voltar = Content.Load<Texture2D>("bot_voltar");
            botao_voltar_click = Content.Load<Texture2D>("bot_voltar_click");
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here
            mouse = Mouse.GetState();

            posicao.X = mouse.X;
            posicao.Y = mouse.Y;            

            switch (tela_atual)
            {
                case Telas.INTRO:

                    if (Colisao_jogar(new Point(mouse.X, mouse.Y)) || Colisao_instrucao(new Point(mouse.X, mouse.Y)))
                    {                       
                        botao_atual = Botoes.CLICK;
                    }
                    else botao_atual = Botoes.NORMAL;

                    if (Colisao_jogar(new Point(mouse.X, mouse.Y)))
                    {
                        if(mouse.LeftButton == ButtonState.Pressed)
                        {
                            //tela_atual = Telas.JOGO;
                        }
                        
                    }                     

                    if (Colisao_instrucao(new Point(mouse.X, mouse.Y)))
                    {
                        if(mouse.LeftButton == ButtonState.Pressed)
                        {
                            tela_atual = Telas.INSTRUCAO;
                        }
                        
                    }
                  
                    break;
                case Telas.INSTRUCAO:
                    if (Colisao_voltar(new Point(mouse.X, mouse.Y)))
                    {
                        botao_atual = Botoes.CLICK;
                    }
                    else botao_atual = Botoes.NORMAL;

                    if (Colisao_voltar(new Point(mouse.X, mouse.Y)))
                    {
                        if (mouse.LeftButton == ButtonState.Pressed)
                        {
                            tela_atual = Telas.INTRO;
                        }

                    }
                    break;
                case Telas.JOGO:
                    jogo.Jogo_inicio();
                    break;
            }
        
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            // TODO: Add your drawing code here
            spriteBatch.Begin();
            switch (tela_atual)
            {
                case Telas.INTRO:
                    spriteBatch.Draw(telaIntroducao, new Vector2(50,50), Color.White);
                    break;
                case Telas.INSTRUCAO:
                    spriteBatch.Draw(telaInstrucao, Vector2.Zero, Color.White);
                    break;
                case Telas.JOGO:
                    spriteBatch.Draw(telaJogo, Vector2.Zero, Color.White);
                    break;
            }
            switch (botao_atual)
            {
                
                case Botoes.NORMAL:
                    if (tela_atual == Telas.INSTRUCAO)
                        spriteBatch.Draw(botao_voltar, new Vector2(670, 550), Color.White); 
                    else{
                        spriteBatch.Draw(botao_jogar, new Vector2(250, 400), Color.White);                    
                        spriteBatch.Draw(botao_instrucoes, new Vector2(450, 400), Color.White);
                    }
                    break;
                case Botoes.CLICK:
                   if (tela_atual == Telas.INSTRUCAO)
                       spriteBatch.Draw(botao_voltar_click, new Vector2(670, 550), Color.White);
                else{
                    if (Colisao_jogar(new Point(mouse.X, mouse.Y)))
                    {
                        spriteBatch.Draw(botao_jogar_click, new Vector2(250, 400), Color.White);
                        spriteBatch.Draw(botao_instrucoes, new Vector2(450, 400), Color.White);
                    }
                    else if (Colisao_instrucao(new Point(mouse.X, mouse.Y)))
                    {
                        spriteBatch.Draw(botao_instrucoes_click, new Vector2(450, 400), Color.White);
                        spriteBatch.Draw(botao_jogar, new Vector2(250, 400), Color.White);
                    }
                }
                    break;
            }
            spriteBatch.Draw(cursor, posicao, new Rectangle(0, 0, 64, 64), Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0.1f);
            spriteBatch.End();

            base.Draw(gameTime);
        }
        private bool Colisao_jogar(Point xy)
        {
            if ((xy.X > 250 && xy.X < 250 + botao_jogar.Width) && (xy.Y > 400 && xy.Y < 400 + botao_jogar.Height))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private bool Colisao_instrucao(Point xy)
        {
            if ((xy.X > 450 && xy.X < 450 + botao_instrucoes.Width) && (xy.Y > 400 && xy.Y < 400 + botao_instrucoes.Height))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private bool Colisao_voltar(Point xy)
        {
            if ((xy.X > 670 && xy.X < 670 + botao_voltar.Width) && (xy.Y > 550 && xy.Y < 550 + botao_voltar.Height))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
      
    }
}
